<?php

namespace app\user\controller;

use app\common\model\Transaction;
use app\common\service\TransactionService;
use app\user\validate\TransactionValidate;
use app\common\attributes\RandyValidate;
use app\common\attributes\Menu;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\App;

class TransactionController  extends UserBaseController
{
    protected Transaction $model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new Transaction();
    }

    #[Inject]
    protected  TransactionService $service ;

    #[menu('查看交易结算日志列表')]
    public function index(): Json
    {
         $data = $this->getParams();
         //当前是否为关联查询
         $this->relationSearch = true;
         //设置过滤方法
         $extendQueryHelper = $this->buildparams();
         $list = $this->service->getTransactionList($extendQueryHelper, $data);
         foreach ($list as $row) {

         }
         return $this->success($list);
    }
     #[menu('查看交易结算日志列表')]
       public function list(  ){
        $data = $this->getParams();
         //当前是否为关联查询
         $this->relationSearch = true;
         //设置过滤方法
          $extendQueryHelper = $this->buildparams();
          $extendQueryHelper->setIsPage(false);
          $list = $this->service->getTransactionList($extendQueryHelper, $data);
           foreach ($list as $row) {

           }
           return $this->success($list);
       }


    #[menu('查看交易结算日志详情')]
    #[RandyValidate(validate: TransactionValidate::class, scene: 'read')]
    public function read( $transaction_id ): Json
    {
        $res = $this->service->getTransactionItem($transaction_id);
        return $this->returnSuccess($res);
    }

    #[menu('删除交易结算日志')]
    #[RandyValidate(validate: TransactionValidate::class, scene: 'del')]
    public function del( $transaction_id ):Json
    {
        $res = $this->service->delTransactionItem( $transaction_id );
        return $this->returnSuccess($res);
    }

    #[menu('更新交易结算日志')]
    #[RandyValidate(validate: TransactionValidate::class, scene: 'update')]
    public function update( $transaction_id ): \think\response\Json
    {
        $data = $this->getParams();
        $res = $this->service->setTransactionItem($transaction_id ,$data);
        return $this->returnSuccess();
    }

    #[menu('新增交易结算日志')]
    #[RandyValidate(validate: TransactionValidate::class, scene: 'save')]
    public function save():Json
    {
        $data = $this->getParams();
        $res = $this->service->addTransactionItem($data);
        return $this->returnSuccess($res);
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    #[menu('新增交易结算日志')]
    #[RandyValidate(validate: TransactionValidate::class, scene: 'save')]
    public function getUseFinishTask() : Json
    {
        $res = $this->service->getUseFinishTask(get_client_id());
        return $this->returnSuccess($res);
    }

    public function today()
    {
        $res = $this->service->today(get_client_id());
        return $this->returnSuccess($res);
    }
}
